begin
	--Deleting all users
	if object_id('meta_users') is not null
	begin
		declare @a_user_name varchar(26),
		@a_user_cursor cursor;
		
		set @a_user_cursor = cursor for
		select user_name from meta_users;
		open @a_user_cursor;
		
		fetch next from @a_user_cursor into @a_user_name;
		
		while @@fetch_status = 0
		begin
			exec ('drop user ' + @a_user_name);
			exec ('drop login ' + @a_user_name);
		
			fetch next from @a_user_cursor into @a_user_name;
		end;
		
		close @a_user_cursor;
		deallocate @a_user_cursor;
	end;

	--Deleting all roles
	if object_id('meta_roles') is not null
	begin
		declare @a_role_name varchar(26),
		@a_role_cursor cursor;

		set @a_role_cursor = cursor for
		select role_name from meta_roles;
		open @a_role_cursor;

		fetch next from @a_role_cursor into @a_role_name;
		
		while @@fetch_status = 0
		begin
			exec ('drop role ' + @a_role_name);
		
			fetch next from @a_role_cursor into @a_role_name;
		end;
		
		close @a_role_cursor;
		deallocate @a_role_cursor;
	end;

	declare @a_table_name varchar(30);
	declare @a_constraint_name varchar(255);
	
	--Deleting all foreign keys
	declare @a_constraint_cursor cursor;
	set @a_constraint_cursor = cursor for
	select table_name, constraint_name from
	information_schema.constraint_table_usage
	where constraint_name in 
	(select constraint_name from information_schema.referential_constraints);
	
	open @a_constraint_cursor;
	
	fetch next from @a_constraint_cursor
	into @a_table_name, @a_constraint_name;
	
	while @@fetch_status = 0 begin
		execute ('alter table ' + @a_table_name
		+ ' drop constraint ' + @a_constraint_name);
		
		fetch next from @a_constraint_cursor
		into @a_table_name, @a_constraint_name;
	end;
	
	close @a_constraint_cursor;
	deallocate @a_constraint_cursor;
	
	--Deleting all tables
	declare	@a_table_cursor cursor;
	set @a_table_cursor = cursor for
	select table_name from information_schema.tables
	where table_type = 'BASE TABLE';
	
	open @a_table_cursor;
	
	fetch next from @a_table_cursor into @a_table_name;
	
	while @@fetch_status = 0 begin
		
		execute ('drop table ' + @a_table_name);
		
		fetch next from @a_table_cursor into @a_table_name;
	end;
	
	close @a_table_cursor;
	deallocate @a_table_cursor;
	
	--Deleting all views
	declare	@a_view_cursor cursor;
	set @a_view_cursor = cursor for
	select table_name from information_schema.tables
	where table_type = 'VIEW';
	
	open @a_view_cursor;
	
	fetch next from @a_view_cursor into @a_table_name;
	
	while @@fetch_status = 0 begin
		
		execute ('drop view ' + @a_table_name);
		
		fetch next from @a_view_cursor into @a_table_name;
	end;
	
	close @a_view_cursor;
	deallocate @a_view_cursor;
	
	--Deleting all procedures
	declare @procedure_name varchar(255);
	
	declare	@a_procedure_cursor cursor;
	set @a_procedure_cursor = cursor for
	select routine_name from information_schema.routines
	where routine_type = 'PROCEDURE';
	
	open @a_procedure_cursor;
	
	fetch next from @a_procedure_cursor into @procedure_name;
	
	while @@fetch_status = 0 begin
		
		execute ('drop procedure ' + @procedure_name);
		
		fetch next from @a_procedure_cursor into @procedure_name;
	end;
	
	close @a_procedure_cursor;
	deallocate @a_procedure_cursor;
	
	--Deleting all functions
	declare @function_name varchar(255);
	
	declare	@a_function_cursor cursor;
	set @a_function_cursor = cursor for
	select routine_name from information_schema.routines
	where routine_type = 'FUNCTION';
	
	open @a_function_cursor;
	
	fetch next from @a_function_cursor into @function_name;
	
	while @@fetch_status = 0 begin
		
		execute ('drop function ' + @function_name);
		
		fetch next from @a_function_cursor into @function_name;
	end;
	
	close @a_function_cursor;
	deallocate @a_function_cursor;
end;